<?php
include 'SignAndCheck.php';
// function do_post_request($url, $data, $optional_headers = null) {
//     $params = array (
//         'http' => array (
//             'method' => 'POST',
//             'content' => http_build_query($data)
//         ),
//         'ssl' => array (
//             'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT,
//         )
//     );
//     if ($optional_headers !== null) {
//         $params ['http'] ['header'] = $optional_headers;
//     }
//     $ctx = stream_context_create ( $params );
//     $fp = @fopen ( $url, 'rb', false, $ctx );
//     if (! $fp) {
//         throw new Exception ( "Problem with $url, $php_errormsg" );
//     }
//     $response = @stream_get_contents ( $fp );
//     if ($response === false) {
//         throw new Exception ( "Problem reading data from $url, $php_errormsg" );
//     }
    
//     $obj = json_decode($response, TRUE);
//     $signature = $obj['sign'];
//     $obj['sign'] = '';
//     $obj['sign_type'] = '';
//     $data = getStr($obj);
//     $re = verify($data, $signature, '../cer/dev_pub.pem');
//     if($re){
//         return $obj;
//     }
//     return json_decode("{'resp_code':'888888','resp_msg':'验签失败'}", TRUE);
// }

function do_post_multipart_request($url, $data, $file_fields = null, $optional_headers = null){
    $cURLHandle = curl_init($url);
    if($file_fields != null){
        foreach ($file_fields as $key => $value){
            $data[$key] = $value;
        }
    }
//     curl_setopt ($cURLHandle, CURLOPT_SSL_VERIFYPEER, 1);
//     curl_setopt ($cURLHandle, CURLOPT_SSL_VERIFYHOST, 2);
//     curl_setopt ($cURLHandle, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($cURLHandle, CURLOPT_POST,true);
    curl_setopt($cURLHandle, CURLOPT_POSTFIELDS, $data);
    curl_setopt($cURLHandle, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2); 
    //设置头文件的信息作为数据流输出
//     curl_setopt($cURLHandle, CURLOPT_HEADER, 0);
    //设置获取的信息以文件流的形式返回，而不是直接输出。
    curl_setopt($cURLHandle, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($cURLHandle, CURLOPT_HTTPHEADER, $optional_headers);
    $result = curl_exec($cURLHandle);
    $php_errormsg = null;
    if (curl_errno($cURLHandle)) {
        $php_errormsg= curl_error($cURLHandle);
    }
    curl_close ($cURLHandle);
    if ($php_errormsg != null) {
        throw new Exception ( "Problem reading data from $url, $php_errormsg" );
    }
    
    $obj = json_decode($result, TRUE);
    $signature = $obj['sign'];
    $obj['sign'] = '';
    $obj['sign_type'] = '';
    $data = getStr($obj);
    $re = verify($data, $signature, '../cer/dev_pub.pem');
    if($re){
        return $obj;
    }
    return json_decode("{'resp_code':'888888','resp_msg':'验签失败'}", TRUE);
}

function do_post_request($url, $data, $optional_headers = null) {
    $cURLHandle = curl_init($url);
    //     curl_setopt ($cURLHandle, CURLOPT_SSL_VERIFYPEER, 1);
    //     curl_setopt ($cURLHandle, CURLOPT_SSL_VERIFYHOST, 2);
    //     curl_setopt ($cURLHandle, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($cURLHandle, CURLOPT_POST,true);
    curl_setopt($cURLHandle, CURLOPT_POSTFIELDS, $data);
    curl_setopt($cURLHandle, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
    //设置头文件的信息作为数据流输出
    //     curl_setopt($cURLHandle, CURLOPT_HEADER, 0);
    //设置获取的信息以文件流的形式返回，而不是直接输出。
    curl_setopt($cURLHandle, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($cURLHandle, CURLOPT_HTTPHEADER, $optional_headers);
    $result = curl_exec($cURLHandle);
    var_dump($result);//此处打印返回值 类型，长度，值。商户自行选择是否打印
    $php_errormsg = null;
    if (curl_errno($cURLHandle)) {
        $php_errormsg= curl_error($cURLHandle);
    }
    curl_close ($cURLHandle);
    if ($php_errormsg != null) {
        throw new Exception ( "Problem reading data from $url, $php_errormsg" );
    }
    
    $obj = json_decode($result, TRUE);
    $signature = $obj['sign'];
    $obj['sign'] = '';
    $obj['sign_type'] = '';
    $data = getStr($obj);
    $re = verify($data, $signature, '../cer/dev_pub.pem');
    if($re){
        return $obj;
    }
    return json_decode("{'resp_code':'888888','resp_msg':'验签失败'}", TRUE);
}
?>